<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Set New Password - Teaching Management System</title>
    <link rel="stylesheet" href="account-login.css">
</head>
<body>
    <div class="container">
        <h2>Set a new password</h2>
        <p style="color: #718096; margin-bottom: 30px; font-size: 14px; line-height: 1.5;">
            Please set a new password to ensure password security
        </p>
        <form id="set-new-password-form">
            <div class="input-group">
                <label for="password">Password</label>
                <input type="password" id="password" placeholder="6-16 digits and letters combination" required>
                <span id="pa-error-message" class="error-message"></span>
            </div>
            
            <div class="input-group">
                <label for="confirm-password">Confirm new password</label>
                <input type="password" id="confirm-password" placeholder="Please enter the new password again" required>
                <span id="cp-error-message" class="error-message"></span>
            </div>
            
            <button type="submit" class="button">
                <span class="button-text">Confirm reset</span>
            </button>
            
            <div id="success-message" class="success-message"></div>
        </form>
        
        <div class="auth-links">
            <p>
                <a href="login.html">Return to login page</a>
            </p>
        </div>
    </div>
    
    <script src="../database.js"></script>
    <script src="account.js"></script>
    <script>
        // Check if there is permission to reset password
        window.onload = function() {
            const resetUserId = localStorage.getItem('resetPassword');
            if (!resetUserId) {
                alert('Invalid access, please first verify your identity');
                window.location.href = 'forgot-password.html';
                return;
            }
        }

        // When leaving the password input field, verify if the password is filled
        document.getElementById('password').addEventListener('blur', () => {
            checkPassword();
        });

        // When leaving the confirm password input field, verify if the confirm password is filled
        document.getElementById('confirm-password').addEventListener('blur', () => {
            checkConfirmPassword();
        });

        document.getElementById('set-new-password-form').addEventListener('submit', (e) => {
            e.preventDefault();
            
            const button = e.target.querySelector('button');
            const buttonText = button.querySelector('.button-text');
            const userid = localStorage.getItem('resetPassword');
            let password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirm-password').value;

            // Check if the information is filled
            if (!checkPassword() || !checkConfirmPassword()) {
                return;
            }

            // Verify if the password and confirm password match
            if (password !== confirmPassword) {
                document.getElementById('cp-error-message').textContent = 'The two passwords do not match, please enter again';
                return;
            }

            // Show loading state
            button.classList.add('loading');
            button.disabled = true;
            buttonText.textContent = 'Resetting...';

            // Simulate network delay
            setTimeout(() => {
                // Clear error messages
                document.getElementById('pa-error-message').textContent = '';
                document.getElementById('cp-error-message').textContent = '';

                // Add salt hash encryption to the password
                password = simpleHash(password, userid);

                // Store the new password in localStorage
                let item = JSON.parse(localStorage.getItem(userid));
                if (!item) {
                    alert('User information exception, please verify again');
                    window.location.href = 'forgot-password.html';
                    return;
                }
                
                item.password = password;
                localStorage.setItem(userid, JSON.stringify(item));

                // Show success message
                document.getElementById('success-message').textContent = 'Password reset successful！Redirecting to login page...';

                // Clear resetPassword
                localStorage.removeItem('resetPassword');

                // Redirect to login page after 2 seconds
                setTimeout(() => {
                    window.location.href = 'login.html';
                }, 2000);
            }, 1000);
        });

        // Add input field animation effect
        document.querySelectorAll('.input-group input').forEach(input => {
input.addEventListener('focus', function() {
this.parentElement.classList.add('focused');
});
input.addEventListener('blur', function() {
if (!this.value) {
this.parentElement.classList.remove('focused');
}
});
});
//Real time verification of password matching
document.getElementById('confirm-password').addEventListener('input', function() {
const password = document.getElementById('password').value;
const confirmPassword = this.value;
const errorElement = document.getElementById('cp-error-message');
if (confirmPassword && password !== confirmPassword) {
errorElement.textContent='Password mismatch'; // Fixed variable name from ErrElement to errorElement
} else {
errorElement.textContent = '';
}
});
    </script>
</body>
</html>